package in.muton.edushare.components.whiteMenu;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

public abstract class WhiteMenu extends Panel {

	private static final long serialVersionUID = -8275354551987694261L;
	private HashMap<String, Boolean> menuLinkStatus = new HashMap<String, Boolean>();
	private IModel<ArrayList<String>> menuModel;
	
	public WhiteMenu(String id, IModel<ArrayList<String>> menuLabelListModel){
		this(id, menuLabelListModel, -1);
	}
	
	
	public WhiteMenu(String id, IModel<ArrayList<String>> menuLabelListModel, int preSelectedMenuIndex) {
		super(id, menuLabelListModel);
		this.menuModel = menuLabelListModel;
		
		//create menuName <--> status hashmap, used to highlight selected one
		for(int i=0; i<menuLabelListModel.getObject().size(); i++){
			if(i==preSelectedMenuIndex)
				menuLinkStatus.put(menuLabelListModel.getObject().get(i), true);
			else
				menuLinkStatus.put(menuLabelListModel.getObject().get(i), false);
		}
		
		//create menu
		ListView<String> lv = new ListView<String>("menuBlock", menuLabelListModel){

			private static final long serialVersionUID = -1416977894881304068L;

			@Override
			protected void populateItem(final ListItem<String> item) {
				
				//set class selected if status = on (true)
				if(menuLinkStatus.get(item.getModelObject()))
					item.add(new AttributeAppender("class", true, new Model<String>("sel"), " "));
				
				//add label version and set visible if selected
				Label lbl = new Label("lbl", item.getModelObject()){
					
					private static final long serialVersionUID = 848118769987655069L;

					@Override
					public boolean isVisible() {
						return menuLinkStatus.get(item.getModelObject());
					}
				};
				
				//add link version and set visible if status inactive
				Link<Void> lnk = new Link<Void>("lnk"){

					private static final long serialVersionUID = -2221215094099969301L;

					@Override
					public void onClick() {
						highlightMenu(item.getModelObject());
						/*
						 * 
						 */
						onMenuClick(item);
					}
					
					@Override
					public boolean isVisible() {
						return !menuLinkStatus.get(item.getModelObject());
					}
					
				};
				lnk.add(new Label("lnkLbl", item.getModelObject()).setRenderBodyOnly(true));
				
				item.add(lnk);
				item.add(lbl);
			}
			
		};
		
		add(lv);
	}
	
	/**
	 * highlights and disables link for the given menu item name
	 * @param menuItemName
	 */
	public void highlightMenu(String menuItemName){
		for(String str: menuLinkStatus.keySet()){
			menuLinkStatus.put(str, false);
		}
		menuLinkStatus.put(menuItemName, true);
	}
	
	/**
	 * highlights and disables link for the given menu item index. Index is zero based, zero being the left most menu item.
	 * @param menuItemIndex
	 */
	public void highlightMenu(int menuItemIndex){
		highlightMenu(this.menuModel.getObject().get(menuItemIndex));
	}

	/**
	 * Override to write the action executed on clicking this menu link.
	 * you can use menuItem.getModelObject() to get menuLabel (list of which sent in constructor)
	 * @param menuItem
	 */
	protected abstract void onMenuClick(ListItem<String> menuItem);

}
